MFC UDP Serial Sample

Introduction

This sample demonstrates how to write a UDP serial application using the MFC socket architecture to communicate with a SG15.

For the purpose of this demonstration, the sample application creates a networking socket it uses to send and receive serial data encapsulated in UDP datagrams from the SG15. The SG15 uses its UDP Client and Server features to communicate.

SG15 Setup

This sample was designed to operate with the SG15 operating in UDP mode.

Configure the SG15

1.       Use the SG15 configuration tool to access the settings of this specific SG15.

2.       Choose the "Network" tab from the Configuration menu.

3.       Configure the SG15 to communicatie through UDP by doing the following:

a.       Check "UDP" in the Connect Mode section.

b.       Enter the IP address of the PC running the sample in the "Remote IP" field.

c.       Specify 9000 as the UDP port number.

d.       Click "Apply now".

How To Build

This sample has been written and tested with Microsoft Visual C++® 6.0. It contains a Developer Studio Workspace file (.dsw), that can be opened in the development environment for editing and compiling.

To build this sample from within Microsoft Visual C++® 6.0

1.       Select File > Open Workspace... from the main menu.

2.       Locate and open the .dsw file for this sample.

3.       Select Build > Rebuild All menu items to compile the sample.

Step-by-Step

1.  Initialize Windows Sockets

The following initialization code should be in the main CWinApp derived object's overridable InitInstance function.

 
if (!AfxSocketInit())
{
   AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
   return FALSE;
}
 

Initializing Windows Sockets is critical. Failing to initialize Windows Sockets will cause all other socket function calls to fail.

2.  Create a local socket

The call to create a socket looks like this:

 
m_MySocket.Create(9000, SOCK_DGRAM)
 

In this sample, the socket is represented by the CAsyncSocket derived class CMySocket. An object of CMySocket type is declared as a private member variable of the main dialog class and named m_MySocket.

m_MySocket is unusable until its Create member function is called, which is done in the main dialog's OnInitDialog overridable function. Since this sample demonstrates using a UDP socket that sends and receives data, a well-known port (9000) and datagram socket type (SOCK_DGRAM) are both supplied to the call to Create. The remaining default parameters of the Create function are sufficient for the application's purposes.

3.  Transmit data

Transmitting data is done by calling the socket's SendTo member functions.

 
m_MySocket.SendTo(Data, Length,
                  PortToSendTo, AddressToSendTo);
 

The SendTo member function is used on connectionless UDP sockets to transmit a datagram to a specific peer. The parameters supplied are: Data which is a pointer to a buffer containing the data to transmit, Length which is the number of bytes of data in the buffer, and PortToSendTo and AddressToSendTo which are the port number and IP address of the SG15 that should receive the data.

4.  Receive data

To retrieve data, the socket's ReceiveFrom member function is called.

 
m_MySocket.ReceiveFrom(Data, sizeof(Data),
                       AddressReceivedFrom,
                       PortReceivedFrom);
 

In the function call the sample provides a buffer (Data) to store the incoming data, the length (sizeof(Data)) of that buffer in bytes, and locations (AddressReceivedFrom and PortReceivedFrom) to store the IP address and port number of the SG15 that sent the data.

With the SG15 setup for UDP, a scanned barcode will be sent directly back to the sample application. The socket is notified that data has arrived for it when it receives the OnReceive notification.

5.  Close the socket

Accomplish this task by calling the following two functions:

 
m_MySocket.ShutDown(CAsyncSocket::both);
m_MySocket.Close();
 

When the sample application is done using the socket, the connection it represents must be terminated properly (ShutDown) and any resources it may be using should be released (Close).

File List

HexDumpData.cpp

HexDumpData.h

MySocket.cpp

MySocket.h

resource.h

StdAfx.cpp

StdAfx.h

UDPSerial.cpp

UDPSerial.dsp

UDPSerial.h

UDPSerial.rc

UDPSerialDlg.cpp

UDPSerialDlg.h

res\UDPSerial.ico

res\UDPSerial.rc2

Keywords

CAsyncSocket::Create; CAsyncSocket::SendTo; CAsyncSocket::ReceiveFrom; CAsyncSocket::OnReceive; CAsyncSocket::ShutDown; CAsyncSocket::Close